package com.tju.resource.oauth.config.initialize;

import cn.hutool.core.io.FileUtil;
import cn.hutool.extra.ftp.Ftp;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tju.resource.model.SysVersions;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import static com.tju.resource.common.GlobalCall.*;

/**
 * 初始化
 *
 * @Author CLS
 * @Date 2022/4/14 19:33
 */
@Slf4j
@Component
public class Initialization implements CommandLineRunner {

    @Override
    public void run(String... args) {
        try {
            QueryWrapper<SysVersions> wrapper = new QueryWrapper<>();
            wrapper.eq("version_code", "move");
            wrapper.eq("current", 1);
            SysVersions versions = versionsMapper.selectOne(wrapper);
            if (versions != null) {
                //文件名
                String fileName = "ten_tian_mes_" + versions.getVersionMark() + ".apk";
                //清空文件夹
                FileUtil.clean(AppPath);
                //连接FTP
                Ftp ftp = new Ftp("tjutech.club", 21, "dt", "BBB123aaa123@");
                //下载远程文件
                // 部署com
                ftp.download("/nginx-1.20.1/html/mes_qoe_app", fileName, FileUtil.file(AppPath + fileName));
                //部署浩洋
                //ftp.download("/nginx-1.20.1/html/mes_haoyang_app", fileName, FileUtil.file(AppPath + fileName));
                //关闭连接
                ftp.close();
            }
        } catch (Exception e) {
            log.error(e.getMessage());
            e.printStackTrace();
        }
    }
}
